﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace test2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);

            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        }
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        public static double stt=2;
        private int so;
        private void Form1_Load(object sender, EventArgs e)
        {
            Random rand = new Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++)
            {
                sb.Append(rand.Next(9).ToString());
            }
            so = int.Parse(sb.ToString());
            CaptchaImage caImage = new CaptchaImage(sb.ToString(), pictureBox1.Width + 30, pictureBox1.Height + 30, "Arial");
            pictureBox1.Image = caImage.Image;
            xlWorkSheet.Cells[1, 1] = "stt";
            xlWorkSheet.Cells[1, 2] = "Họ và tên";
            xlWorkSheet.Cells[1, 3] = "Số CMND";
            xlWorkSheet.Cells[1, 4] = "Email";
            xlWorkSheet.Cells[1, 5] = "Ngày sinh";
            xlWorkSheet.Cells[1, 6] = "Nghề nghiệp";
            xlWorkSheet.Cells[1, 7] = "Địa chỉ";
            xlWorkSheet.Cells[1, 8] = "Giới tính";
            xlWorkSheet.Cells[1, 9] = "Giá đấu";
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.Form1_Load(this, new EventArgs());
        }
        
        private void btnAgree_Click(object sender, EventArgs e)
        {
            //try
            //{
                int testedInt = int.Parse(txtCaptchaText.Text);
                if (testedInt == so)
                {
                    if (!((txtTen.Text == "") | (txtCMND.Text == "") | (txtDiachi.Text == "") | (txtNghe.Text == "") | (txtEmail.Text == "")|!(radioNam.Checked|radioNu.Checked)|(txtNgaySinh.Text=="")))
                    {
                        if(radioNam.Checked)
                        {
                            xlWorkSheet.Cells[stt, 8] = "Nam";
                        }
                        if(radioNu.Checked)
                        {
                            xlWorkSheet.Cells[stt, 8] = "Nữ";
                        }
                    Form2 frm2 = new Form2();                   
                    frm2.Owner=this;
                    this.Hide();
                    frm2.Show();
                    xlWorkSheet.Cells[stt, 1] = (stt-1).ToString();
                    xlWorkSheet.Cells[stt, 2] = txtTen.Text;
                    xlWorkSheet.Cells[stt, 3] = txtCMND.Text;
                    xlWorkSheet.Cells[stt, 4] = txtEmail.Text;
                    xlWorkSheet.Cells[stt, 5] = txtNgaySinh.Text;
                    xlWorkSheet.Cells[stt, 6] = txtNghe.Text;
                    xlWorkSheet.Cells[stt, 7] = txtDiachi.Text;
                    txtCaptchaText.Clear();
                    txtCMND.Clear();
                    txtDiachi.Clear();
                    txtTen.Clear();
                    txtNgaySinh.Clear();
                    txtEmail.Clear();
                    txtNghe.Clear();
                    this.Form1_Load(this, new EventArgs());
                    }
                    else
                    {
                        MessageBox.Show("Bạn chưa điền đủ thông tin");
                    }
                }
                else
                {
                    MessageBox.Show("Bạn đã nhập sai mã xác nhận!");
                    this.Form1_Load(this, new EventArgs());
                }
           // }
            //catch (Exception)
            //{
            //    MessageBox.Show("Bạn đã nhập sai mã xác nhận!");
            //    this.Form1_Load(this, new EventArgs());
            //}
        }

       public void Gia(string x)
       {
           xlWorkSheet.Cells[stt, 9]=x;
       }
       public void tangstt()
       {
           stt++;
       }



       private void Admin(object sender, EventArgs e)
       {
           Form4 frm4 = new Form4();
           frm4.Owner=this;
           frm4.Show();
           this.Hide();
       }

      public void XUATDULIEU()
      {
          xlWorkBook.SaveAs("ketqua.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
          xlWorkBook.Close(true, misValue, misValue);
          xlApp.Quit();

          releaseObject(xlWorkSheet);
          releaseObject(xlWorkBook);
          releaseObject(xlApp);
      }

     
                
        
    }
}
